{% extends 'student/base.html' %} {% block title %}Courses{% endblock %} {% block body %} {% load static %}
<script>
    var requireJS = [];
    var request_url = "{% url 'dashboard:student_courses_grades_api'  %}";
    loadJS(requireJS, "{% static 'dashboard/views/charts.js'%}");
</script>

<input type="hidden" id="myVar" name="variable" readonly value="{{ student_predictions.courses }}">

<input type="hidden" id="total" name="variable" readonly value="{{ student_courses_num_pages }}">
<main class="animated fadeIn row">
    <div class="col-md-12">
        <div class="card" style="margin-top: 10px">
            <div class="card-header">
                <h3 class="card-title mb-0">Courses Performance</h3>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="row col-sm-9">
                        <div class="col-xl-2 col-md-3">

                            <div class="dropdown">
                                <select class="form-control" id="year-selector">
                                    <option id="yTitle" value="" selected>Year</option>
                                    {% for year in years %}
                                        <option value="{{ year.id }}"
                                                id="year_{{ forloop.counter }}">{{ year.name }}</option>
                                    {% endfor %}
                                </select>


                            </div>
                        </div>
                    </div>

                    <div class="col-md-3">
                        <div class="btn-toolbar float-right" role="toolbar" aria-label="Toolbar with button groups">
                            <div class="btn-group mr-3" data-toggle="buttons" aria-label="First group">
                                {% for term in terms %} 
                                    <label class="btn btn-outline-secondary">
                                        <input type="radio" id="rb" name="options"
                                               value="{{ term.id }}"> {{ term.name }}
                                    </label>
                                {% endfor %}
                            </div>
                        </div>
                    </div>

                </div>

                <div class="wrapper mx-auto">
                    <canvas id="canvas-1" class="chart"></canvas>
                </div>
                
            </div>
        </div>
    </div>

    <div class="col-md-12">
        <div class="card" style="margin-top: 10px">
            <div class="card-header">
                <div class="row">
                    <div class="col-md-8 col-sm-12">
                        <h3 class="card-title mb-0">Courses</h3>
                    </div>
                    <div class="input-group col-md-4 col-sm-12">
                        <input type="text" id="search_input" name="input1-group2" class="form-control" placeholder="Course Name">
                        <span class=" input-group-btn ">
                            <button type="button" class="btn btn-primary " id="search_btn">
                                <i class="fa fa-search"></i>
                            </button>
                        </span>

                    </div>
                </div>
            </div>
            <div class="card-body" id="courses_results">
                {% include "./pagination/courses.html" %}
            </div>
        </div>
    </div>
</main>
<script>
    document.getElementById("lis_1").parentElement.classList.add("active");
    var search_val = "";

    function loadResults(event) {
        var id = event.target.id;
        if (document.getElementById(id).parentElement.classList.contains("active") != true) {
            page = document.getElementById(id).innerHTML;
            if (isNaN(page)) {
                page = id;
            }

            url = "{% url 'dashboard:student_courses_paginator' %}?page=" + page;

            if (search_val != "") {
                url += "&keyword=" + search_val;
            }
            $.ajax({
                type: "GET",
                url: url,
                success: function (data) {
                    $('#courses_results').html(data);
                    if (isNaN(id) != true)
                        id = "lis_" + id;
                    document.getElementById(id).parentElement.classList.add("active")

                },

                error: function (response, error) {
                    alert(response.responseText);
                }
            });
        }
    }

    function searchResults(e) {
        search_val = $("#search_input").val();

        $.ajax({
            type: "GET",
            url: "{% url 'dashboard:student_courses_paginator' %}?keyword=" + search_val,
            success: function (data) {
                $('#courses_results').html(data);
                document.getElementById("lis_1").parentElement.classList.add("active");
            },

            error: function (response, error) {
                alert(response.responseText);
            }
        });
    }
    $(document).ready(function () {
        var input_val;
        $("#search_btn").click(function (e) {
            input_val = $("#search_input").val();
            if (input_val !== search_val) {
                searchResults();
            }
        });
        $("#search_input").keypress(function (e) {
            input_val = $("#search_input").val();
            if (input_val !== search_val) {
                var key_pressed = e.which;
                if (key_pressed == 13) {
                    searchResults();
                }
            }
        });
    });
</script>

{% endblock %}